package com.renjun.smweb.controller.NewsController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.service.NewsService.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by lrs on 2017/2/28.
 */
@Controller
@RequestMapping(value = "/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 添加系统公告
     * @author lrs
     * @param request params:{title:标题, starttime:开始时间, endtime:结束时间, content:内容}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/addNews",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M12-1", desc = "添加系统公告")
    public JSONObject addNews(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = newsService.addNews(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 修改系统公告
     * @author lrs
     * @param request params:{id:系统公告id, title:标题, starttime:开始时间, endtime:结束时间, content:内容}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/modifyNews",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M12-2", desc = "修改系统公告")
    public JSONObject modifyNews(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = newsService.modifyNews(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 查询系统公告/系统公告列表
     * @author lrs
     * @param request params:{title:标题, ename:发布人, starttime:开始时间, endtime:结束时间, content:内容}
     * @param response data:{[title:标题, ename:发布人, addtime:发布时间, starttime:开始时间, endtime:结束时间, status:状态]}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/queryNews",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M12-3", desc = "查询系统公告/系统公告列表")
    public JSONObject queryNews(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = newsService.queryNews(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 查看系统公告/系统公告详情
     * @author lrs
     * @param request params:{id:系统公告id}
     * @param response data:{title:标题, ename:发布人, addtime:发布时间, starttime:开始时间, endtime:结束时间, status:状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/getNews",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M12-4", desc = "查看系统公告/系统公告详情")
    public JSONObject getNews(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = newsService.getNews(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 删除系统公告
     * @author lrs
     * @param request params:{id:系统公告id}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/cancelNews",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M12-5", desc = "删除系统公告")
    public JSONObject cancelNews(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = newsService.cancelNews(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 恢复系统公告
     * @author lrs
     * @param request params:{id:系统公告id}
     * @param response data:{message:请求描述,status:请求状态}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/recoverNews",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M12-6", desc = "恢复系统公告")
    public JSONObject recoverNews(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = newsService.recoverNews(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 管理员登录后系统公告弹出框信息
     * @author lrs
     * @param request params:{}
     * @param response data:{title:标题, addtime:发布时间, content:内容}
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/loginQueryNewsList",method = RequestMethod.POST)
    @AuthorityDeclaration(id = "M12-7", desc = "管理员登录后系统公告弹出框信息")
    public JSONObject loginQueryNewsList(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs;
        try {
            rs = newsService.loginQueryNewsList(params);
        }catch (Exception e) {
            e.printStackTrace();
//            rs.put("message", e.getMessage());
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }
}
